package com.hzlx.service.impl;

import com.hzlx.dao.TBusinessInfoDao;
import com.hzlx.dao.TOrderInfoDao;
import com.hzlx.dao.impl.TBusinessInfoDaoImpl;
import com.hzlx.dao.impl.TOrderInfoDaoImpl;
import com.hzlx.entity.RespBean;
import com.hzlx.entity.TOrderInfo;
import com.hzlx.service.BusinessInfoService;
import com.hzlx.entity.TBusinessInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author 86166
 * @version 1.0
 * @description: TODO
 * @date 2023/4/10 19:08
 */
public class BusinessInfoServiceImpl implements BusinessInfoService {
    private static TBusinessInfoDao tBusinessInfoDao = new TBusinessInfoDaoImpl();
    private static TOrderInfoDao tOrderInfoDao = new TOrderInfoDaoImpl();
    @Override
    public String login(HttpServletRequest request, HttpServletResponse response) {

        String url="";
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        TBusinessInfo tBusinessInfo = tBusinessInfoDao.selectByUserNameAndPassword(userName, password);
        if (tBusinessInfo==null){
            url="pages/business/login.jsp";
        }else {
            request.getSession().setAttribute("tBusinessInfo",tBusinessInfo);
            url="businessHome.do";
        }
        return url;
    }

    @Override
    public String register(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String address = request.getParameter("address");
        String tel = request.getParameter("tel");
        TBusinessInfo tBusinessInfo = new TBusinessInfo();
        tBusinessInfo.setName(name);
        tBusinessInfo.setUserName(userName);
        tBusinessInfo.setPassword(password);
        tBusinessInfo.setAddress(address);
        tBusinessInfo.setTel(tel);
        int rows = tBusinessInfoDao.insert(tBusinessInfo);
        String url = "";
        if (rows>0){
            url = "pages/business/login.jsp";
        }else {
            url="pages/business/register.jsp";
        }
        return url;
    }

    @Override
    public void goHome(HttpServletRequest request,HttpServletResponse response) {
        TBusinessInfo tBusinessInfo = (TBusinessInfo) request.getSession().getAttribute("tBusinessInfo");
        //获取预计收入（待支付）
        request.setAttribute("projectedIncome",tBusinessInfoDao.selectProjectedIncome(tBusinessInfo.getId()));
        //获取待处理订单数 待接单 待支付
        request.setAttribute("pendingOrderNo",tBusinessInfoDao.selectPendingOrderNo(tBusinessInfo.getId()));
        //获取待接单订单列表
        request.setAttribute("pendingOrderList",tOrderInfoDao.selectPendingOrderList(tBusinessInfo.getId()));

    }

    @Override
    public String takeOrder(HttpServletRequest request) {
        RespBean respBean;
        //修改指定订单状态
        int rows = tOrderInfoDao.updateOrderStatus(request.getParameter("orderNo"));
        if (rows>0){
            //修改成功
            respBean = RespBean.success();
            System.out.println(respBean.getCode()+" "+respBean.getMessage());

        }else {
            respBean= RespBean.error(10010,"修改订单状态失败");
            //修改失败
        }
        Gson gson = new Gson();
        return gson.toJson(respBean);
    }

    @Override
    public void getOrderInfo(HttpServletRequest request) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        TBusinessInfo tBusinessInfo = (TBusinessInfo) request.getSession().getAttribute("tBusinessInfo");
        List<TOrderInfo> orderInfos = tOrderInfoDao.selectOrderListAll(tBusinessInfo.getId());
        request.setAttribute("orderInfos",orderInfos);
    }
}
